﻿using System;
using System.Collections.Generic;
using agsXMPP;
using agsXMPP.protocol.client;
using sharpxbot.Core.Security;
using sharpxbot.Plugins.Base;
using sharpxbot.Plugins.Context;

namespace sharpxbot.Core.Plugins
{
    public class NickSpamFilterPlugin : AbstractPlugin
    {
        public const int KickThreshHold = 2;
        private static readonly Guid PluginGuid = new Guid("6feb5ed3-291f-4572-914b-88e6051555d1");

        public NickSpamFilterPlugin(IPluginContext context)
            : base(context)
        {
            cache = new Dictionary<Jid, NickChangeInfo>();
        }

        public override PluginPermissions NeededPermissions
        {
            get { return PluginPermissions.Everyone; }
        }

        public override string Name
        {
            get { return "NickSpamFilterPlugin"; }
        }

        public override string Author
        {
            get { return "tillias"; }
        }

        public override Guid GUID
        {
            get { return PluginGuid; }
        }

        public override string Description
        {
            get { return "Restricts too frequent nickname change for group-chat"; }
        }

        public override string Usage
        {
            get { return "This is automatic plugin. No commands allowed"; }
        }

        public override void ProcessPresence(Presence presence)
        {
            if (presence == null)
                return;
            if (presence.MucUser == null)
                return;
            if (presence.MucUser.Item == null)
                return;
            if (presence.MucUser.Item.Jid == null)
                return;

            Jid realJid = presence.MucUser.Item.Jid;

            if (presence.Type == PresenceType.available)
            {
                NickChangeInfo existingInfo = null;
                string currentNick = presence.From.Resource;

                if (!cache.TryGetValue(realJid, out existingInfo))
                {
                    NickChangeInfo info = new NickChangeInfo()
                    {
                        Previous = currentNick,
                        Current = currentNick,
                        ChangesCounter = 0
                    };
                    cache[realJid] = info;
                }
                else
                {
                    if (currentNick != existingInfo.Current)
                    {
                        existingInfo.Previous = existingInfo.Current;
                        existingInfo.Current = currentNick;
                        ++existingInfo.ChangesCounter;
                    }

                    if (existingInfo.ChangesCounter > KickThreshHold)
                    {
                        existingInfo.ChangesCounter = 0; // Force reset counter right before kick
                        PerformKick(presence.From);
                    }
                }
            }
        }

        public override void ProcessMessage(Message message)
        {
            //Nothing todo here
        }


        private void PerformKick(Jid mucJid)
        {
            Context.GroupChats.KickUser(mucJid.Bare, mucJid.Resource, "You've exceeded allowed nick changes count in this room");
        }

        Dictionary<Jid, NickChangeInfo> cache;


    }

    public class NickChangeInfo
    {
        public string Previous { get; set; }
        public string Current { get; set; }
        public int ChangesCounter { get; set; }
    }

}
